from typing import List


class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        len1 = len(nums1)
        len2 = len(nums2)
        if len1 > len2:
            return self.findMedianSortedArrays(nums2, nums1)
        leftTotal = (len1 + len2 + 1) // 2
        start = 0
        end = len(nums1)
        inf = float('inf')
        while start <= end:
            mid1 = (start + end) // 2 #  先定nums1的分隔线
            mid2 = leftTotal - mid1 # 查出nums2的分隔线
            # 分别找出两个分格线的左右元素
            l1 = -inf if mid1 == 0 else nums1[mid1 - 1]
            r1 = inf if mid1 == len1 else nums1[mid1]
            l2 = -inf if mid2 == 0 else nums2[mid2 - 1]
            r2 = inf if mid2 == len2 else nums2[mid2]
            if l1 > r2: # l1 > l2 说明 mid1 偏大，需左移
                end = mid1 - 1
            elif l2 > r1: # l2 > r1 说明 mid2 偏大，需右移
                start = mid1 + 1
            else:
                if (len1 + len2) % 2 == 1:
                    return max(l1, l2)
                else:
                    return (max(l1, l2) + min(r1, r2)) / 2

            
            

            